#!/usr/bin/env python3
"""
方案2: API逆向工程 - 监控网络请求找到完整Key Features的数据源
通过Selenium + Chrome DevTools Protocol监控所有网络请求
"""

import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import time
import json
import requests

def monitor_network_requests():
    """监控网络请求，找到Key Features的API端点"""
    
    # 启用Chrome DevTools Protocol日志
    chrome_options = Options()
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-dev-shm-usage')
    chrome_options.add_argument('--disable-gpu')
    chrome_options.add_argument('--window-size=1920,1080')
    chrome_options.add_argument('--enable-logging')
    chrome_options.add_argument('--v=1')
    # chrome_options.add_argument('--headless')  # 注释掉以便观察
    
    # 启用Chrome DevTools Protocol和性能日志
    chrome_options.add_argument('--enable-logging')
    chrome_options.add_argument('--log-level=0')
    chrome_options.add_experimental_option('useAutomationExtension', False)
    chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])
    
    # 启用性能日志
    chrome_options.set_capability('goog:loggingPrefs', {'performance': 'ALL'})
    
    service = Service(ChromeDriverManager().install())
    driver = webdriver.Chrome(service=service, options=chrome_options)
    
    # 启用网络域
    driver.execute_cdp_cmd('Network.enable', {})
    driver.execute_cdp_cmd('Page.enable', {})
    
    network_requests = []
    
    def capture_request(message):
        """捕获网络请求"""
        if message['method'] == 'Network.responseReceived':
            url = message['params']['response']['url']
            method = message['params']['response']['status']
            response_type = message['params']['response']['mimeType']
            
            # 只记录感兴趣的请求
            if any(keyword in url.lower() for keyword in ['api', 'json', 'feature', 'specification', 'model', 'product', 'configure']):
                network_requests.append({
                    'url': url,
                    'method': method,
                    'type': response_type,
                    'timestamp': time.time()
                })
    
    try:
        print("🔍 开始API逆向工程 - 监控网络请求...")
        
        # 访问主页面
        url = "https://www.ford.co.uk/shop/build-your-ford/ngc2/ice2/model?catalogId=WAEGB-CGE-2022-FocusMCAGBR202450&series=ACMAA_VS-DH&planId=c"
        print(f"🌐 访问页面: {url}")
        
        driver.get(url)
        
        # 等待页面初始加载
        print("⏳ 等待页面初始加载...")
        WebDriverWait(driver, 15).until(
            EC.presence_of_element_located((By.CSS_SELECTOR, 'div[data-cy][class*="ProductTile"]'))
        )
        
        # 处理Cookie同意
        print("🍪 处理Cookie同意...")
        try:
            cookie_buttons = driver.find_elements(By.CSS_SELECTOR, 'button[id*="cookie"], button[class*="cookie"], button[data-cy*="cookie"]')
            for btn in cookie_buttons:
                if btn.is_displayed():
                    btn.click()
                    time.sleep(1)
                    break
        except:
            pass
        
        # 收集页面加载时的网络请求
        time.sleep(5)
        logs = driver.get_log('performance')
        
        print(f"📊 页面加载完成，收集到 {len(logs)} 个性能日志条目")
        
        # 解析网络请求日志
        api_requests = []
        for log in logs:
            message = json.loads(log['message'])
            
            if message['message']['method'] == 'Network.responseReceived':
                response = message['message']['params']['response']
                url = response['url']
                status = response['status']
                mime_type = response.get('mimeType', '')
                
                # 筛选API相关的请求
                if (status == 200 and 
                    ('json' in mime_type.lower() or 
                     any(keyword in url.lower() for keyword in ['api', 'service', 'data', 'ajax', 'fetch']))):
                    
                    api_requests.append({
                        'url': url,
                        'status': status,
                        'mime_type': mime_type,
                        'headers': response.get('headers', {}),
                        'timestamp': log['timestamp']
                    })
        
        print(f"🎯 找到 {len(api_requests)} 个可能的API请求:")
        for i, req in enumerate(api_requests):
            print(f"  {i+1}. {req['url']}")
            print(f"     状态: {req['status']}, 类型: {req['mime_type']}")
        
        # 现在点击Titanium的View Specifications
        print(f"\n🎯 点击Titanium View Specifications，监控新的网络请求...")
        
        try:
            # 找到Titanium车型
            titanium_element = driver.find_element(By.CSS_SELECTOR, 'div[data-cy="Titanium"]')
            spec_button = titanium_element.find_element(By.CSS_SELECTOR, 'button[data-cy="product-tile-overlay"]')
            
            # 清除之前的日志
            driver.get_log('performance')
            
            # 点击按钮
            ActionChains(driver).move_to_element(titanium_element).perform()
            time.sleep(1)
            driver.execute_script("arguments[0].click();", spec_button)
            
            print("✅ 已点击View Specifications按钮")
            
            # 等待可能的API调用
            time.sleep(5)
            
            # 收集点击后的网络请求
            new_logs = driver.get_log('performance')
            print(f"📊 点击后收集到 {len(new_logs)} 个新的性能日志条目")
            
            # 解析新的网络请求
            click_api_requests = []
            for log in new_logs:
                message = json.loads(log['message'])
                
                if message['message']['method'] == 'Network.responseReceived':
                    response = message['message']['params']['response']
                    url = response['url']
                    status = response['status']
                    mime_type = response.get('mimeType', '')
                    
                    if (status == 200 and 
                        ('json' in mime_type.lower() or 
                         any(keyword in url.lower() for keyword in ['api', 'service', 'data', 'ajax', 'fetch']))):
                        
                        click_api_requests.append({
                            'url': url,
                            'status': status,
                            'mime_type': mime_type,
                            'headers': response.get('headers', {}),
                            'timestamp': log['timestamp']
                        })
            
            print(f"🎯 点击后找到 {len(click_api_requests)} 个新的API请求:")
            for i, req in enumerate(click_api_requests):
                print(f"  {i+1}. {req['url']}")
                print(f"     状态: {req['status']}, 类型: {req['mime_type']}")
            
            # 尝试获取这些API的响应内容
            print(f"\n🔍 尝试获取API响应内容...")
            
            promising_apis = []
            
            for req in click_api_requests:
                try:
                    print(f"\n🌐 请求API: {req['url']}")
                    
                    # 模拟浏览器请求
                    headers = {
                        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',
                        'Accept': 'application/json, text/plain, */*',
                        'Accept-Language': 'en-US,en;q=0.9',
                        'Referer': url
                    }
                    
                    response = requests.get(req['url'], headers=headers, timeout=10)
                    
                    if response.status_code == 200:
                        try:
                            # 尝试解析JSON
                            data = response.json()
                            print(f"✅ 成功获取JSON数据，大小: {len(str(data))} 字符")
                            
                            # 搜索Key Features相关数据
                            data_str = json.dumps(data).lower()
                            
                            feature_keywords = [
                                'keyfeatures', 'key_features', 'features',
                                'specifications', 'specs',
                                'alloy wheels', 'touchscreen', 'keyless',
                                'led headlamps', 'quickclear'
                            ]
                            
                            found_keywords = [kw for kw in feature_keywords if kw in data_str]
                            
                            if found_keywords:
                                print(f"🎉 API包含Key Features相关数据！找到关键词: {found_keywords}")
                                
                                promising_apis.append({
                                    'url': req['url'],
                                    'data': data,
                                    'keywords': found_keywords,
                                    'data_size': len(str(data))
                                })
                                
                                # 保存这个有希望的API数据
                                filename = f"./release/api_data_{len(promising_apis)}.json"
                                with open(filename, 'w', encoding='utf-8') as f:
                                    json.dump({
                                        'api_url': req['url'],
                                        'found_keywords': found_keywords,
                                        'response_data': data
                                    }, f, ensure_ascii=False, indent=2)
                                
                                print(f"💾 API数据已保存到: {filename}")
                            else:
                                print(f"❌ API不包含Key Features相关数据")
                                
                        except json.JSONDecodeError:
                            print(f"❌ 响应不是有效的JSON")
                        except Exception as e:
                            print(f"❌ 处理JSON失败: {e}")
                    else:
                        print(f"❌ API请求失败，状态码: {response.status_code}")
                        
                except Exception as e:
                    print(f"❌ 请求API失败: {e}")
            
            # 总结结果
            print(f"\n📊 API逆向工程结果总结:")
            print(f"  页面加载时API请求: {len(api_requests)} 个")
            print(f"  点击后新增API请求: {len(click_api_requests)} 个")
            print(f"  包含Key Features的API: {len(promising_apis)} 个")
            
            if promising_apis:
                print(f"\n🎉 找到有希望的API端点:")
                for i, api in enumerate(promising_apis):
                    print(f"  {i+1}. {api['url']}")
                    print(f"     数据大小: {api['data_size']} 字符")
                    print(f"     包含关键词: {api['keywords']}")
                
                # 尝试在最有希望的API中查找完整的Key Features
                best_api = max(promising_apis, key=lambda x: len(x['keywords']))
                print(f"\n🔍 分析最有希望的API: {best_api['url']}")
                
                data = best_api['data']
                
                # 递归搜索Key Features
                def find_features_in_data(obj, path=""):
                    features = []
                    if isinstance(obj, dict):
                        for key, value in obj.items():
                            if 'feature' in key.lower():
                                features.append(f"Found features at {path}.{key}: {value}")
                            features.extend(find_features_in_data(value, f"{path}.{key}"))
                    elif isinstance(obj, list):
                        for idx, item in enumerate(obj):
                            features.extend(find_features_in_data(item, f"{path}[{idx}]"))
                    elif isinstance(obj, str):
                        # 检查是否包含我们期望的Key Features
                        expected_features = [
                            "Driver Assistance Technologies",
                            "LED Rear Lights",
                            "Electrically Operated",
                            "Grille With Chrome",
                            "Dual-Zone Electronic",
                            "Premium Sensico",
                            "Manual 4-Way Driver",
                            "Lane-Keeping Aid",
                            "Pre-Collision Assist",
                            "TFT Ice Blue",
                            "Thatcham Alarm"
                        ]
                        
                        for expected in expected_features:
                            if expected.lower() in obj.lower():
                                features.append(f"Found expected feature '{expected}' at {path}: {obj}")
                    
                    return features
                
                found_features = find_features_in_data(data)
                
                if found_features:
                    print(f"🎉 在API数据中找到 {len(found_features)} 个相关特性:")
                    for feature in found_features[:10]:  # 只显示前10个
                        print(f"  • {feature}")
                else:
                    print(f"❌ 未在API数据中找到期望的Key Features")
                
            else:
                print(f"❌ 未找到包含Key Features的API端点")
                print(f"可能需要进入更深层的配置流程才能获取完整数据")
            
            # 保存完整的网络分析结果
            result = {
                'page_url': url,
                'initial_api_requests': api_requests,
                'click_api_requests': click_api_requests,
                'promising_apis_count': len(promising_apis),
                'promising_apis': [{'url': api['url'], 'keywords': api['keywords']} for api in promising_apis]
            }
            
            with open('./release/network_analysis_result.json', 'w', encoding='utf-8') as f:
                json.dump(result, f, ensure_ascii=False, indent=2)
            
            print(f"\n💾 完整的网络分析结果已保存到: ./release/network_analysis_result.json")
            
        except Exception as e:
            print(f"❌ 点击操作失败: {e}")
            import traceback
            traceback.print_exc()
        
    except Exception as e:
        print(f"❌ 监控过程中出现异常: {e}")
        import traceback
        traceback.print_exc()
        
    finally:
        print("🧹 清理资源...")
        driver.quit()

if __name__ == "__main__":
    monitor_network_requests()
